import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  suggestionList: {},
  currentSuggestion: {},
  searchSuggestionCondition: {
    pageSize: 10,
    title: "",
    status: ""
  }
};

const getters = {
  suggestionList: state => state.suggestionList,
  currentSuggestion: state => state.currentSuggestion,
  searchSuggestionCondition: state => state.searchSuggestionCondition
};

const actions = {
  getSuggestionPageS({ commit }, param) {
    return Api.getSuggestionPage(param).then(ret => {
      commit(types.UPDATE_SUGGESTION_LIST, ret.data.data);
      return ret;
    });
  },
  getSuggestionS({ commit }, id) {
    return Api.getSuggestion({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_SUGGESTION, ret.data.data);
      return ret;
    });
  },
  addOrUpdateSuggestionS({ commit }, payload) {
    return Api.addOrUpdateSuggestion({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteSuggestionS({ commit }, param) {
    return Api.deleteSuggestion(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_CURRENT_SUGGESTION](state, payload) {
    console.log(payload);
    state.currentSuggestion = payload;
  },
  [types.UPDATE_SUGGESTION_LIST](state, payload) {
    state.suggestionList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
